import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: "/", name: "latory", component: () => import("../views/latory.vue"),
    children: [
      { path: "/index", name: "index", component: () => import("../components/Index.vue") },
      { path: "/menu", name: "menu", component: () => import("../components/Menu.vue") },
      { path: "/role", name: "role", component: () => import("../components/Role.vue") },
      { path: "/user", name: "user", component: () => import("../components/User.vue") },
      { path: "/category", name: "category", component: () => import("../components/Category.vue") },
      { path: "/specs", name: "specs", component: () => import("../components/Specs.vue") },
      { path: "/goods", name: "goods", component: () => import("../components/Goods.vue") }
    ]

  },
  { path: "/login", name: "login", component: () => import("../components/login.vue") }





]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  const no_log = ['/login']
  if (no_log.indexOf(to.path) == -1) {
    var tokenString = localStorage.getItem('log_list') || '{}'
    var token = JSON.parse(tokenString).token
    if (token) {
      var menu_list = JSON.parse(tokenString).menus_url
      menu_list.push('/index')
      menu_list.push('/login')

      if (menu_list.indexOf(to.path) == -1) {
        next('/index')
      }else{
        next()
      }


    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
